﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Imaginecup.util.vertex2D
{

    public class Vertex2DPipeMan 
    {
        private Matrix m_BaseWorldMat; // 기본적으로 변환되는 월드 행렬
        private Matrix m_ViewMat;
        private Matrix m_ProjectMat;

        //카메라정보.
        private Vector3 m_Position; // 1024 : 768 1:1매칭 시키기위해 카메라를 뒤로 물린다.
        private Vector3 m_LookAt;
        private Vector3 m_UpVec;

        private Vector2 m_ScreenSize;

        private static Vertex2DPipeMan renderPipeMan = null;

        private Vertex2DPipeMan()
        {}

        public static Vertex2DPipeMan GetRenderPipeMan()
        {
            if( renderPipeMan == null )
            {
                renderPipeMan = new Vertex2DPipeMan();
            }
            return renderPipeMan;
        }

        public Vector2 GetScreenSize()
        {
            return m_ScreenSize;
        }

        public void SetScreenSize( Vector2 screenSize )
        {
            m_ScreenSize = screenSize;
        }

        
        public void InitPipeMan2D()
        {
            InitBaseWorldMat();
            InitViewMat();
            InitProjectionMat();
        }
        private void InitViewMat()
        {
            m_Position = new Vector3(0 , 0 ,  m_ScreenSize.Y / 2);
            m_LookAt = Vector3.Zero;
            m_UpVec = Vector3.Up;

            m_ViewMat = Matrix.CreateLookAt(m_Position, m_LookAt, m_UpVec);
        }

        private void InitBaseWorldMat()
        {
            m_BaseWorldMat = Matrix.CreateTranslation(new Vector3(-m_ScreenSize.X / 2 , m_ScreenSize.Y / 2, 0));
        }



        private void InitProjectionMat()
        {  
            
            //w
            //  h
            //  w = aspect * h
            //  h = cot(fov/2)



            // * 수식 좌표y = h/z * y' * height/2 + (원점 + height / 2) 
            // 계산하면 y = 1 / z * y' * 384 + (0 + 384 ) 
            // 인대 y = 768일때 y' = 384이면 1:1매칭이 된다.
            //
            // y축을 기준(y의 화면이 1:1 매칭이 되게)으로 하기위해
            // z = 384이 된다. (그래서 카메라를 384뒤로 간다.)//월드좌표 변환을 300 이동시킨다)
            //                            fov = 90  = cot(45) = 1              
            //                                                         

            m_ProjectMat = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver2, m_ScreenSize.X / m_ScreenSize.Y, 1, 10000f);
        }

        public Matrix GetBaseWorldMat()
        {
            return m_BaseWorldMat;
        }

        public Matrix GetViewMat()
        {
            return m_ViewMat;
        }

        public Matrix GetProjectionMat()
        {
            return m_ProjectMat;
        }
    }
}